package com.example.myapplication;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import com.example.myapplication.Config.NetWork;
import com.example.myapplication.apter.Imglistmap;

import java.util.ArrayList;

public class GuidePageActivity extends AppCompatActivity {

    private ViewPager vp;
    private String TAG = "TAG";
    private TextView homepage;
    private TextView network;
    private View yuan1;
    private View yuan2;
    private View yuan3;
    private View yuan4;
    private View yuan5;
    private LinearLayout setwork;
    private SharedPreferences guidePageActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide_page);
        //设置初始化
        guidePageActivity = getSharedPreferences("GuidePageActivity", Context.MODE_PRIVATE);
        boolean isFirst = guidePageActivity.getBoolean("isFirst", true);

        if(isFirst){
            initvew();
            //引导页
            guidePage();
            //进入主页
            enterthehomepage();
            //网络设置
            networksettings();

            SharedPreferences.Editor edit = guidePageActivity.edit();
            edit.putBoolean("isFirst",false);
            edit.apply();

        }else {
            Intent intent = new Intent(GuidePageActivity.this, MainActivity.class);
            GuidePageActivity.this.startActivity(intent);
            finish();
        }

    }

    private void networksettings() {
        network.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(GuidePageActivity.this);
                View inflate = LayoutInflater.from(GuidePageActivity.this).inflate(R.layout.item_networksettings, null);
                EditText ip = inflate.findViewById(R.id.ip);
                EditText port = inflate.findViewById(R.id.port);
                ip.setText(NetWork.IP);
                port.setText(NetWork.PORT);
                builder.setView(inflate);
                builder.setTitle("网络设置");
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });

                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String ed_ip = ip.getText().toString();
                        String ed_port = port.getText().toString();
                        NetWork.IP = ed_ip;
                        NetWork.PORT = ed_port;
                    }
                });
                builder.create().show();
            }
        });
    }

    private void enterthehomepage() {
        homepage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GuidePageActivity.this, MainActivity.class);
                GuidePageActivity.this.startActivity(intent);
            }
        });
    }

    private void guidePage() {
        ArrayList<Integer> imglist = new ArrayList<>();
        imglist.add(R.drawable.img_yi);
        imglist.add(R.drawable.img_er);
        imglist.add(R.drawable.img_san);
        imglist.add(R.drawable.img_si);
        imglist.add(R.drawable.img_wu);

        ArrayList<View> yuanlist = new ArrayList<>();
        yuanlist.add(yuan1);
        yuanlist.add(yuan2);
        yuanlist.add(yuan3);
        yuanlist.add(yuan4);
        yuanlist.add(yuan5);
        vp.setAdapter(new Imglistmap(imglist, this));
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //设置圆点
                for (int i = 0; i < yuanlist.size(); i++) {
                    if (position == i) {
                        yuanlist.get(i).setBackground(getDrawable(R.drawable.re_hei));
                    } else {
                        yuanlist.get(i).setBackground(getDrawable(R.drawable.re_hou));
                    }
                }

                //显示网络
                if (position == imglist.size() - 1) {
                    setwork.setVisibility(View.VISIBLE);
                } else {
                    setwork.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initvew() {
        vp = findViewById(R.id.vp);
        homepage = findViewById(R.id.homepage);
        network = findViewById(R.id.Network);
        setwork = findViewById(R.id.setwork);
        yuan1 = findViewById(R.id.yuan1);
        yuan2 = findViewById(R.id.yuan2);
        yuan3 = findViewById(R.id.yuan3);
        yuan4 = findViewById(R.id.yuan4);
        yuan5 = findViewById(R.id.yuan5);


    }
}